package com.gxkj.drm.controllers;


import com.gxkj.drm.entitys.PushUser;
import com.gxkj.drm.entitys.PushUserOperateLog;
import com.gxkj.drm.service.UserService;
import com.gxkj.drm.util.LoginHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class BaseController {

    protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);

   @Autowired
    protected UserService userService;

    /**
     * 添加用户操作日志
     * @param request
     * @param operation
     * @param ext
     */
    protected void userLog(final HttpServletRequest request, String operation, String cluster, String ext) {
        PushUser user = this.getUser(request);
        PushUserOperateLog puol = new PushUserOperateLog();
        puol.setOperateUid(user.getId());
        puol.setOperater(user.getNickName());
        puol.setOperateIp(user.getLoginIp());
        puol.setOperation(operation);
        puol.setOperateCluster(cluster);
        puol.setOperateTime(new Date());
        puol.setExt(ext);
        this.userService.addUserOperateLog(puol);
    }

    protected PushUser getUser(final HttpServletRequest request) {
        return LoginHelper.getLoginUser(request);
    }
}
